/**
 * Created by LiYang on 16-9-27 下午2:07.
 *
 */

package com.example.db.annotation;


import com.example.db.utils.CommonUtil;

public class Column {
    private String value;
    private ColumnMeta.Type type;
    private int length;
    private boolean isNull;
    private boolean primary;
    private boolean increment;

    public Column(String fieldName) {
        this.value = CommonUtil.toUnderline(fieldName);
    }

    public Column(String fieldName, ColumnMeta columnMeta) {
        this.value = CommonUtil.toUnderline(fieldName);
        this.type = columnMeta.type();
        this.length = columnMeta.length();
        this.isNull = columnMeta.isNull();
        this.primary = columnMeta.primary();
        this.increment = columnMeta.increment();
    }

    public ColumnBuilder getBuilder() {
        return new ColumnBuilder();
    }

    public String getValue() {
        return value;
    }

    public ColumnMeta.Type getType() {
        return type;
    }

    public int getLength() {
        return length;
    }

    public boolean isNull() {
        return isNull;
    }

    public boolean isPrimary() {
        return primary;
    }

    public boolean isIncrement() {
        return increment;
    }


    public class ColumnBuilder {

        public ColumnBuilder setType(ColumnMeta.Type type) {
            Column.this.type = type;
            return this;
        }

        public ColumnBuilder setLength(int length) {
            Column.this.length = length;
            return this;
        }

        public ColumnBuilder setNull(boolean isNull) {
            Column.this.isNull = isNull;
            return this;
        }

        public ColumnBuilder setPrimary(boolean primary) {
            Column.this.primary = primary;
            return this;
        }

        public ColumnBuilder setIncrement(boolean increment) {
            Column.this.increment = increment;
            return this;
        }

        public Column apply() {
            if (Column.this.type == null) {
                Column.this.type = ColumnMeta.Type.TEXT;
            }
            return Column.this;
        }
    }
}
